package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.DeliveryManner;
import com.ecoolex.chinadutyfree.retailer.entity.DeliveryManner.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddDeliveryMannerDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditDeliveryMannerDTO;
import com.ecoolex.chinadutyfree.retailer.ws.DeliveryService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.google.common.collect.Lists;


@GraphQL2Module(type = Type.Admin, description = "配送方式管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
	key = DeliveryMannerGraphql.AUTH,
	name = "配送方式管理")
public class DeliveryMannerGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-DELIVERY-MANNER";

	@Reference
	private DeliveryService service;

	private static final String AUTH_GET = AUTH + "-GET";

	@QueryField(field = "deliveryManner", description = "获取配送方式详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取配送方式详情")
	public DeliveryManner get(@NonNull Integer deliveryMannerId, AdminMemberAuthority user) {
		user.assertLogin();
		return service.getDeliveryMannerById(deliveryMannerId);
	}

	private static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "deliveryMannerList", description = "查询配送方式列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询配送方式列表")
	public Pagination<DeliveryManner> query(String name, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertLogin();
		return service.queryDeliveryManner(name, sorts, page);
	}

	private static final String AUTH_ADD = AUTH + "-ADD";

	@MutationField(field = "addDeliveryManner", description = "添加配送方式")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加配送方式")
	public DeliveryManner add(@NonNull AddDeliveryMannerDTO add, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return service.addDeliveryManner(add, user.userName());
	}

	private static final String AUTH_EDIT = AUTH + "-EDIT";

	@MutationField(field = "editDeliveryManner", description = "编辑配送方式")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑配送方式")
	public DeliveryManner add(@NonNull EditDeliveryMannerDTO edit, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		DeliveryManner old = service.getDeliveryMannerById(edit.getDeliveryMannerId());
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "配送方式不存在");
		// 对象拷贝
		BeanUtil.copy(edit,
			old,
			Lists.newArrayList(Column.deleted, Column.createUser, Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return service.updateDeliveryManner(edit, user.userName());
	}

	private static final String AUTH_DEL = AUTH + "-DEL";

	@MutationField(field = "delDeliveryManner", description = "删除配送方式")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除配送方式")
	public Boolean del(@NonNull Integer deliveryMannerId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);

		DeliveryManner record = service.getDeliveryMannerById(deliveryMannerId);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "配送方式不存在");
		service.deleteDeliveryManner(record, user.userName());

		return true;
	}
}

